﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace Chenxin.Lib.Business.DAL
{
    public class ContractPrintInfo
    {
        public ContractPrintInfo()
        {
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.ContractPrintInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tb_ContractPrint(");
            strSql.Append("ContractPrint_ID,ContractPrint_Date,ContractPrint_Reason,Contract_ID,Contract_Code,User_ID,User_Code,User_Name)");
            strSql.Append(" values (");
            strSql.Append("@ContractPrint_ID,@ContractPrint_Date,@ContractPrint_Reason,@Contract_ID,@Contract_Code,@User_ID,@User_Code,@User_Name)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());

            #region 参数
            db.AddInParameter(dbCommand, "@ContractPrint_ID", DbType.Guid, model.ContractPrint_ID);
            db.AddInParameter(dbCommand, "@ContractPrint_Date", DbType.DateTime, model.ContractPrint_Date);
            db.AddInParameter(dbCommand, "@ContractPrint_Reason", DbType.AnsiString, model.ContractPrint_Reason);
            db.AddInParameter(dbCommand, "@Contract_Id", DbType.Guid, model.Contract_Id);
            db.AddInParameter(dbCommand, "@Contract_Code", DbType.AnsiString, model.Contract_Code);
            db.AddInParameter(dbCommand, "@User_ID", DbType.Guid, model.User_ID);
            db.AddInParameter(dbCommand, "@User_Code", DbType.AnsiString, model.User_Code);
            db.AddInParameter(dbCommand, "@User_Name", DbType.AnsiString, model.User_Name);
            #endregion

            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.ContractPrintInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_ContractPrint set ");
            strSql.Append("ContractPrint_Date=@ContractPrint_Date,");
            strSql.Append("ContractPrint_Reason=@ContractPrint_Reason,");
            strSql.Append("Contract_ID=@Contract_ID,");
            strSql.Append("Contract_Code=@Contract_Code,");
            strSql.Append("User_ID=@User_ID,");
            strSql.Append("User_Code=@User_Code,");
            strSql.Append("User_Name=@User_Name");
            strSql.Append(" where ContractPrint_ID=@ContractPrint_ID ");

            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());

            #region 参数
            db.AddInParameter(dbCommand, "@ContractPrint_ID", DbType.Guid, model.ContractPrint_ID);
            db.AddInParameter(dbCommand, "@ContractPrint_Date", DbType.DateTime, model.ContractPrint_Date);
            db.AddInParameter(dbCommand, "@ContractPrint_Reason", DbType.AnsiString, model.ContractPrint_Reason);
            db.AddInParameter(dbCommand, "@Contract_Id", DbType.Guid, model.Contract_Id);
            db.AddInParameter(dbCommand, "@Contract_Code", DbType.AnsiString, model.Contract_Code);
            db.AddInParameter(dbCommand, "@User_ID", DbType.Guid, model.User_ID);
            db.AddInParameter(dbCommand, "@User_Code", DbType.AnsiString, model.User_Code);
            db.AddInParameter(dbCommand, "@User_Name", DbType.AnsiString, model.User_Name);
            #endregion

            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid ContractPrint_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_ContractPrint ");
            strSql.Append(" where ContractPrint_ID=@ContractPrint_ID ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());


            db.AddInParameter(dbCommand, "@ContractPrint_ID", DbType.Guid, ContractPrint_ID);

            db.ExecuteNonQuery(dbCommand);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ContractPrintInfo GetModel(Guid ContractPrint_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ContractPrint_ID,ContractPrint_Date,ContractPrint_Reason,Contract_ID,Contract_Code,User_ID,User_Code,User_Name");
            strSql.Append(" FROM tb_ContractPrint ");
            strSql.Append(" where Contract_Id=@Contract_Id ");

            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ContractPrint_ID", DbType.Guid, ContractPrint_ID);
            Chenxin.Lib.Business.Entity.ContractPrintInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ContractPrintInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.ContractPrintInfo model = new Chenxin.Lib.Business.Entity.ContractPrintInfo();
            if (dataReader["ContractPrint_Id"].ToString() != "")
            {
                model.ContractPrint_ID = new Guid(dataReader["ContractPrint_Id"].ToString());
            }
            if (dataReader["ContractPrint_Date"].ToString() != "")
            {
                model.ContractPrint_Date = DateTime.Parse(dataReader["ContractPrint_Date"].ToString());
            }
            model.ContractPrint_Reason = dataReader["ContractPrint_Reason"].ToString();

            if (dataReader["Contract_Id"].ToString() != "")
            {
                model.Contract_Id = new Guid(dataReader["Contract_Id"].ToString());
            }
            model.Contract_Code = dataReader["Contract_Code"].ToString();
            if (dataReader["User_ID"].ToString() != "")
            {
                model.User_ID = new Guid(dataReader["User_ID"].ToString());
            }
            model.User_Code = dataReader["User_Code"].ToString();
            model.User_Name = dataReader["User_Name"].ToString();
            return model;
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ContractPrint_ID,ContractPrint_Date,ContractPrint_Reason,Contract_ID,Contract_Code,User_ID,User_Code,User_Name");
            strSql.Append(" FROM tb_ContractPrint ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public static DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ContractPrint_ID,ContractPrint_Date,ContractPrint_Reason,Contract_ID,Contract_Code,User_ID,User_Code,User_Name");
            strSql.Append(" FROM tb_ContractPrint ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);

            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, "tb_ContractPrint");
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }
    }
}
